package com.gitee.wsl.ext.list

import kotlin.random.Random


/**
 * Returns a list containing a specified number of random elements from this list.
 *
 * @param random the random number generator to use for selecting random elements.
 * @param amount the number of random elements to select.
 * @return a list containing the specified number of random elements.
 * @throws IllegalArgumentException if the amount is less than 0.
 */
fun <T> List<T>.random(random: Random, amount: Int): List<T> {
    require(amount >= 0) { "amount cannot be less than 0" }
    if (amount == 0) return emptyList()

    val list = ArrayList<T>(amount)
    repeat(amount) {
        val index = random.nextInt(lastIndex + 1)
        list.add(this[index])
    }
    return list
}